package com.sg.service.biz.app.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.biz.app.req.GenerateSemanticContentReqDto;
import com.sg.dto.biz.app.res.GenerateSemanticContentRespDto;
import com.sg.dto.biz.app.res.ImplementAcceptFieldRespDto;
import com.sg.dto.integration.component.QuerySemanticEngineDetailReqDto;
import com.sg.dto.integration.component.QuerySemanticEngineDetailRespDto;
import com.wicket.okrcomponent.integration.TemplateEngineService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbStatusData {

    @Resource
    private TemplateEngineService fwCompTemplateEngineClient;

    /**
     * code:generateSemanticInstance
     * name:M3-生成语义内容
     * desc:undefined
     * gen by moon at 10/27/2022, 3:51:24 AM
     **/
    @Trace(operationName = "M3-生成语义内容")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public GenerateSemanticContentRespDto generateSemanticContent(GenerateSemanticContentReqDto reqDto) {
        // {systemDateTime}{randomNumberDigits}{targetContentName}批次

        boolean bRetFlag = false;
        GenerateSemanticContentRespDto retData = new GenerateSemanticContentRespDto();
        String retConent = "";
        QuerySemanticEngineDetailReqDto querySemanticEngineDetailReqDto = new QuerySemanticEngineDetailReqDto();
        querySemanticEngineDetailReqDto.setTemplateSemanticEngineCode("GENERATE_EVA_TEMP_BATCH_NAME");
        QuerySemanticEngineDetailRespDto querySemanticEngineDetailRespDto = fwCompTemplateEngineClient.querySemanticEngineDetail(querySemanticEngineDetailReqDto).getData();
        if (querySemanticEngineDetailRespDto != null && querySemanticEngineDetailRespDto.getTemplateEngineTemplate() != null) {
            String systemDateTime = "\\{systemDateTime}";
            String randomNumberDigits = "\\{randomNumberDigits}";
            String targetContentName = "\\{targetContentName}";
            if (reqDto.getTargetContentName() != null && !reqDto.getTargetContentName().isEmpty()) {
                bRetFlag = true;
                retConent = querySemanticEngineDetailRespDto.getTemplateEngineTemplate().replaceAll(targetContentName, reqDto.getTargetContentName());
                retConent = retConent.replaceAll(systemDateTime, CommonFunctionHelper.date2Str(CommonFunctionHelper.getCurrentTime()));
                Random random = new Random();
                String result = "";
                for (int i = 0; i < reqDto.getRandomNumberDigits(); i++) {
                    result += random.nextInt(10);
                }
                retConent = retConent.replaceAll(randomNumberDigits, result);
            } else {
                retConent = querySemanticEngineDetailRespDto.getTemplateEngineTemplate().replaceAll(targetContentName, "---");
            }

            if (retConent != null && !retConent.isEmpty()) {
                retConent = retConent.replaceAll("---", "");
                retConent = retConent.replaceAll("（）", "");
                retData.setObjectName(retConent);
            }

            if (bRetFlag) {
                return retData;
            }

        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M3执行接收字段入参（特殊方法）
     * desc:undefined
     * gen by moon at 4/20/2023, 1:33:59 AM
     **/
    @Trace(operationName = "M3执行接收字段入参（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldRespDto implementAcceptField(ImplementAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldRespDto.class);
    }
    //手工接入方法
}
